﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Jumpton.SFA.Server.BusinessRule;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections;
using System.Web.Hosting;
using System.Configuration;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

using System.Diagnostics;



public partial class Reports_ExportPhoto : PageBase
{
    string STARTIME;
    string ENDTIME;
    protected void Page_Load(object sender, EventArgs e)
    {
        STARTIME = BusinessLogic.GetDateTime(Request.QueryString["BeginTime"], "yyyyMMddHHmmss", "yyyy-MM-dd");
        ENDTIME = BusinessLogic.GetDateTime(Request.QueryString["EndTime"], "yyyyMMddHHmmss", "yyyy-MM-dd");
        if (!IsPostBack)
        {
            lblStarTime.Text = Convert.ToDateTime(STARTIME).ToShortDateString();
            lblEndTime.Text = Convert.ToDateTime(ENDTIME).AddDays(-1).ToShortDateString();
            lblName.Text = Request.QueryString["user"] == "" ? "所有" : Request.QueryString["user"];
        }
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        Export();
        string name = STARTIME + "到" + ENDTIME + "图片";
        dlZipDir( name);
    }

    /// <summary>
    /// 根基筛选条件导出PDF 
    /// </summary>
    private void Export()
    {

        dicMStream = new Dictionary<string, byte[]>();
        //删除过期文件
        DeleteFolder(ConfigurationManager.AppSettings["ExportPdfPath"]);
        PhotoManager manager = new PhotoManager();
        string starTime = Request.QueryString["BeginTime"];
        string endTime = Request.QueryString["EndTime"];
        string user = Request.QueryString["user"];
        string customerName = Request.QueryString["CutomerName"];
        string where = "";
        if (user != "")
        {
            where = " AND V.USER_ID='" + user + "'";
        }

        if (customerName != "")
        {
            where += " AND (C.NAME = '" + customerName + "' OR C.CODE =  '" + customerName + "')";
        }

        if (starTime != "" && endTime != "")
        {
            //where += " AND V.START_TIME BETWEEN " + BusinessLogic.GetStringDateTimeDecimal(starTime) + "000000 AND " + BusinessLogic.GetStringDateTimeDecimal(endTime) + "240000";
            where += " AND V.START_TIME BETWEEN " + BusinessLogic.GetStringDateTimeDecimal(starTime) + " AND " + BusinessLogic.GetStringDateTimeDecimal(endTime);
        }
        DataTable datatable = manager.GetPhotos(MyStateManager.DomainID, where).Tables[0];
        //门店名称或门店代码已选(只有一个门店)
        if (datatable.Rows.Count > 0)
        {
            if (datatable.Rows.Count > 100)
            {
                Response.Write("<script>alert('图片数量过大，请重新选择条件再导出!')");
            }
            var query1 = from temp in datatable.AsEnumerable()
                             .Select(w => w.Field<String>("customerName")).Distinct()
                         select temp;

            DataTable dt_Cp = datatable.AsEnumerable().Where(s => s.Field<bool>("CONTENDER") == true).CopyToDataTable();//竞品
            DataTable dt_Kp = datatable.AsEnumerable().Where(s => s.Field<bool>("CONTENDER") == false).CopyToDataTable();//本品

            var dt_Cp_Query = from temp in dt_Cp.AsEnumerable().Select(s => s.Field<string>("customerName")).Distinct() select temp;
            string[] dt_Cp_Names = dt_Cp_Query.ToArray();
            var dt_Kp_Query = from temp in dt_Kp.AsEnumerable().Select(s => s.Field<string>("customerName")).Distinct() select temp;
            string[] dt_Kp_Names = dt_Kp_Query.ToArray();

            if (dt_Cp_Names.Length > dt_Kp_Names.Length)
            {

            }
            else
            {
                for (int i = 0, k = dt_Kp_Names.Length; i < k; i++)
                {
                    string dt_Kp_Name = dt_Kp_Names[i];
                    IEnumerable<DataRow> customerQuery = from Customertemp in dt_Kp.AsEnumerable()
                                                         where Customertemp.Field<string>("customerName") == dt_Kp_Name
                                                         select Customertemp;
                    DataTable dt_Kp_Temp = customerQuery.CopyToDataTable<DataRow>();
                    DataTable dt_Cp_Temp = new DataTable();
                    if (dt_Cp_Names.Contains<string>(dt_Kp_Name))
                    {
                        IEnumerable<DataRow> customerQuery1 = from Customertemp in dt_Cp.AsEnumerable()
                                                              where Customertemp.Field<string>("customerName") == dt_Kp_Name
                                                              select Customertemp;
                        dt_Cp_Temp = customerQuery1.CopyToDataTable<DataRow>();
                    }
                    ExportPdfForCustomer(dt_Kp_Name, dt_Cp_Temp, dt_Kp_Temp);
                }
            }

            string[] customers = query1.ToArray(); //存放数据中的客户名称
            #region 余亚蕾的导出PDF

            /**
             * 
            if (customers.Length > 0)
            {
                //根据客户名称获取数据
                foreach (string customername in customers)
                {

                    IEnumerable<DataRow> customerQuery = from Customertemp in datatable.AsEnumerable()
                                                         where Customertemp.Field<string>("customerName") == customername
                                                         select Customertemp;
                    DataTable customerdt = customerQuery.CopyToDataTable<DataRow>();//每个客户的数据
                    //根据每个客户到一张pdf
                    ExportPdfForCustomer(customername, customerdt);
                }
            }
             */
            #endregion
            #region
            //select temp1;  //query1是string [] 保存的是customerName
            //string[] customers = query1.ToArray();
            //DateTime time1 = Convert.ToDateTime(STARTIME);
            //DateTime time2 = Convert.ToDateTime(ENDTIME);
            //for (DateTime t = time1; t <= time2; t = t.AddDays(1))
            //{
            //    IEnumerable<DataRow> query = from temp in datatable.AsEnumerable()
            //                                 where BusinessLogic.GetDateTime
            //                                 (temp.Field<Int64>("START_TIME").ToString(), "yyyyMMddHHmmss", "yyyy-MM-dd") == t.ToString("yyyy-MM-dd")
            //                                 select temp;

            //    if (query.Count<DataRow>() > 0)
            //    {
            //        DataTable dt = query.CopyToDataTable<DataRow>();
            //        var query1 = from temp1 in dt.AsEnumerable().Select(w => w.Field<String>("customerName")).Distinct()
            //                     select temp1;  //query1是string [] 保存的是customerName
            //        string[] customers = query1.ToArray();
            //        if (customers.Length > 0)
            //        {
            //            foreach (string customername in customers)
            //            {
            //                IEnumerable<DataRow> customerQuery = from Customertemp in dt.AsEnumerable()
            //                                                     where Customertemp.Field<string>("customerName") == customername
            //                                                     select Customertemp;    //cutomerQuery保存的是customerName相同的数据
            //                if (customerQuery.Count<DataRow>() > 0)
            //                {
            //                    DataTable dt2 = customerQuery.CopyToDataTable<DataRow>();
            //                    if (dt2.Rows.Count > 0)
            //                    {
            //                        ArrayList arraylist = new ArrayList();
            //                        for (int i = 0; i < dt2.Rows.Count; i++)
            //                        {
            //                            if (dt2.Rows[i] != null && dt2.Rows[i]["PHOTO_PATH"].ToString() != "")
            //                            {
            //                                arraylist.Add(ConfigurationManager.AppSettings["ImagePath"] + dt2.Rows[i]["PHOTO_PATH"]);
            //                            }
            //                        }
            //                        if (arraylist.Count > 0)
            //                        {
            //                            ExportPdf(PATH, customername, t.ToString(), arraylist);
            //                        }
            //                    }
            //                }
            //            }
            //        }

            //    }
            //}
            #endregion
            Response.Write("<script>alert('导出成功！');</script>");
        }
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="customerName"></param>
    /// <param name="dt_Cp">竞品</param>
    /// <param name="dt_Kp">本品</param>
    private void ExportPdfForCustomer(string customerName, DataTable dt_Cp, DataTable dt_Kp)
    {

        var brandquery = from brandtemp in dt_Kp.AsEnumerable().Select(w => w.Field<String>("brandName")).Distinct()
                         select brandtemp;
        string[] brands = brandquery.ToArray();
        DateTime time1 = Convert.ToDateTime(STARTIME);
        DateTime time2 = Convert.ToDateTime(ENDTIME);
        TimeSpan ts1 = new TimeSpan(time1.Ticks);
        TimeSpan ts2 = new TimeSpan(time2.Ticks);
        TimeSpan ts = ts1.Subtract(ts2).Duration();
        int days = ts.Days;
        String[] pathList = new String[brands.Length];
        List<string> cp_photos = new List<string>();
        List<string> cp_notes = new List<string>();
        if (brands.Length > 0)
        {
            for (int m = 0; m < brands.Length; m++)
            {
                IEnumerable<DataRow> brandQuery = from Brandtemp in dt_Kp.AsEnumerable()
                                                  where Brandtemp.Field<string>("brandName") == brands[m]
                                                  select Brandtemp;
                DataTable branddt = brandQuery.CopyToDataTable<DataRow>(); //每个品牌总天的数据
                StringBuilder brandpaths = new StringBuilder();
                
                for (DateTime t = time1; t <= time2; t = t.AddDays(1))
                {
                    IEnumerable<DataRow> adayquery = from temp in branddt.AsEnumerable()
                                                     where BusinessLogic.GetDateTime
                                                     (temp.Field<Int64>("START_TIME").ToString(), "yyyyMMddHHmmss", "yyyy-MM-dd") == t.ToString("yyyy-MM-dd")
                                                     select temp;
                    if (adayquery.Count<DataRow>() > 0)
                    {
                        DataTable adaydt = adayquery.CopyToDataTable<DataRow>(); //每个品牌每天的数据
                        StringBuilder onedayPaths = new StringBuilder();
                        for (int n = 0; n < adaydt.Rows.Count; n++)
                        {
                            if (adaydt.Rows[n] != null || adaydt.Rows[n]["PHOTO_PATH"].ToString() != "")
                            {
                                onedayPaths.Append(ConfigurationManager.AppSettings["ImagePath"] + adaydt.Rows[n]["PHOTO_PATH"] + "|");
                            }
                        }
                        for (int n = 0; n < dt_Cp.Rows.Count; n++)
                        {
                            if (dt_Cp.Rows[n] != null || dt_Cp.Rows[n]["PHOTO_PATH"].ToString() != "")
                            {
                                //onedayPaths.Append(ConfigurationManager.AppSettings["ImagePath"] + dt_Cp.Rows[n]["PHOTO_PATH"] + "|");
                                cp_photos.Add(ConfigurationManager.AppSettings["ImagePath"] + dt_Cp.Rows[n]["PHOTO_PATH"]);
                                cp_notes.Add(dt_Cp.Rows[n]["Note"].ToString());
                            }
                        }
                        brandpaths.Append(onedayPaths + t.ToShortDateString() + ";");//一个品牌1天的图片路径及当天的日期
                    }
                }
                pathList[m] = brandpaths + "&" + brands[m];
            }
        }
        ExportPdf(ref customerName, pathList, dt_Kp.Rows[0]["Note"].ToString(),cp_photos,cp_notes);
    }


    #region 余亚蕾代码
    /// <summary>
    /// 为每个客户导出一张Pdf
    /// </summary>
    /// <param name="dt">每个客户总天的数据</param>
    private void ExportPdfForCustomer(string customerName, DataTable cutomerdt)
    {
        var brandquery = from brandtemp in cutomerdt.AsEnumerable().Select(w => w.Field<String>("brandName")).Distinct()
                         select brandtemp;
        string[] brands = brandquery.ToArray();
        DateTime time1 = Convert.ToDateTime(STARTIME);
        DateTime time2 = Convert.ToDateTime(ENDTIME);
        TimeSpan ts1 = new TimeSpan(time1.Ticks);
        TimeSpan ts2 = new TimeSpan(time2.Ticks);
        TimeSpan ts = ts1.Subtract(ts2).Duration();
        int days = ts.Days;
        String[] pathList = new String[brands.Length];
        List<string> cp_photos = new List<string>();
        List<string> cp_notes = new List<string>();
        if (brands.Length > 0)
        {
            for (int m = 0; m < brands.Length; m++)
            {
                IEnumerable<DataRow> brandQuery = from Brandtemp in cutomerdt.AsEnumerable()
                                                  where Brandtemp.Field<string>("brandName") == brands[m]
                                                  select Brandtemp;
                DataTable branddt = brandQuery.CopyToDataTable<DataRow>(); //每个品牌总天的数据
                StringBuilder brandpaths = new StringBuilder();
                for (DateTime t = time1; t <= time2; t = t.AddDays(1))
                {
                    IEnumerable<DataRow> adayquery = from temp in branddt.AsEnumerable()
                                                     where BusinessLogic.GetDateTime
                                                     (temp.Field<Int64>("START_TIME").ToString(), "yyyyMMddHHmmss", "yyyy-MM-dd") == t.ToString("yyyy-MM-dd")
                                                     select temp;
                    if (adayquery.Count<DataRow>() > 0)
                    {
                        DataTable adaydt = adayquery.CopyToDataTable<DataRow>(); //每个品牌每天的数据
                        StringBuilder onedayPaths = new StringBuilder();
                        for (int n = 0; n < adaydt.Rows.Count; n++)
                        {
                            if (adaydt.Rows[n] != null || adaydt.Rows[n]["PHOTO_PATH"].ToString() != "")
                            {
                                onedayPaths.Append(ConfigurationManager.AppSettings["ImagePath"] + adaydt.Rows[n]["PHOTO_PATH"] + "|");
                            }
                        }
                        brandpaths.Append(onedayPaths + t.ToShortDateString() + ";");//一个品牌1天的图片路径及当天的日期
                    }
                }
                pathList[m] = brandpaths + "&" + brands[m];
            }
        }
        ExportPdf(ref customerName, pathList, cutomerdt.Rows[0]["Note"].ToString(),cp_photos,cp_notes);


    }
    #endregion
    /// <summary>
    /// 为每个客户导出pdf
    /// </summary>
    /// <param name="customername">客户名</param>
    /// <param name="pathlist">图片路径</param>
    /// <param name="note">备注</param>
    /// <param name="path">PDF存放路径</param>
    //private void ExportPdf(string customerName, string[] pathlist, string note, string path)
    //{
    //    try
    //    {
    //        Document document = new Document();
    //        string filepath =createFile(path)+ "\\" + customerName.Replace("\n","")+ ".pdf";
    //        PdfWriter writer= PdfWriter.GetInstance(document, new FileStream(filepath, FileMode.Create ));

    //        MemoryStream ms =  new MemoryStream();
    //        //PdfWriter writer = PdfWriter.GetInstance(document, ms);
    //        document.Open();
    //        BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    //        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new BaseColor(0, 0, 0));
    //        document.Add(new Paragraph("店名： " + customerName, fontChinese));
    //        const float w = 250f;
    //        const float h = 260f;
    //        //输出图片到PDF文件 
    //        if (pathlist.Length  > 0)
    //        {
    //            for (int i = 0; i < pathlist.Length ; i++)
    //            {
    //                string branddayspath = pathlist[i];  //一个品牌多天数据信息
    //                string[] branddayspathList = branddayspath.Split(';');
    //                if (branddayspathList.Length > 0)
    //                {
    //                    //document.Add(new Paragraph("品牌： " + branddayspathList[branddayspathList.Length - 1].Substring (1), fontChinese));
    //                    for (int j = 0; j < branddayspathList.Length - 1; j++)
    //                    {
    //                        document.Add(new Paragraph("品牌： " + branddayspathList[branddayspathList.Length - 1].Substring(1), fontChinese));
    //                        bool flag = false;
    //                       string[] brandadaypathList=branddayspathList[j].Split ('|'); //一个品牌一天的数据
    //                       if(brandadaypathList .Length >0)
    //                       {
    //                           flag = true;
    //                           document.Add(new Paragraph("时间： " + brandadaypathList[brandadaypathList.Length - 1],fontChinese));
    //                           for (int m = 0; m < brandadaypathList.Length - 1; m++)
    //                           {

    //                               string photoPath = brandadaypathList[m];
    //                               if (!File.Exists(photoPath))
    //                               {
    //                                   continue;
    //                               }
    //                               iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(photoPath);
    //                               if (jpeg01 == null)
    //                               {
    //                                   continue;
    //                               }
    //                               if (jpeg01.Height > jpeg01.Width)
    //                                   jpeg01.SetAbsolutePosition(45f + (m % 2) * (5f + w), document.PageSize.Height - (h + 95f) * (1 + m / 2));
    //                               else
    //                                   jpeg01.SetAbsolutePosition(45f + (m % 2) * (5f + w), document.PageSize.Height - (h + 20f) * (1 + m / 2));
    //                               jpeg01.ScaleToFit(w, h);
    //                               document.Add(jpeg01);
    //                           }
    //                       }

    //                       PdfContentByte cb = writer.DirectContent;
    //                       if (note.Length > 28)
    //                       {
    //                           Phrase footer1 = new Phrase("备注: " + note.Substring(0, 30), fontChinese);
    //                           ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer1,
    //                           250, document.Bottom + 26f, 0);
    //                           Phrase footer2 = new Phrase(note.Substring(31), fontChinese);
    //                           ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER , footer2,
    //                           250, document.Bottom + 14f, 0);
    //                       }
    //                       else
    //                       {
    //                           Phrase footer = new Phrase("备注: " + note, fontChinese);
    //                           ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer,
    //                           150, document.Bottom + 20, 0);
    //                       }
    //                       if (flag)
    //                           document.NewPage();//每个品牌每天的信息占一页
    //                    }
    //                }


    //            }
    //        }
    //        document.Close();

    //    }
    //    catch (DocumentException de)
    //    {
    //        Response.Write(de.ToString());
    //    }
    //}




    /// <summary>
    /// PDF保存到内存中，并返回
    /// </summary>
    /// <param name="customerName">PDF名称</param>
    /// <param name="pathlist"></param>
    /// <param name="note"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    private MemoryStream ExportPdf(ref string customerName, string[] pathlist, string note,List<string> cp_photos,List<string> cp_notes)
    {
        MemoryStream pdfms = new MemoryStream();
        try
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, pdfms);
            document.Open();
            BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new BaseColor(0, 0, 0));
            document.Add(new Paragraph("店名： " + customerName, fontChinese));
            const float w = 250f;
            const float h = 260f;
            //输出图片到PDF文件 
            if (pathlist.Length > 0)
            {
                for (int i = 0; i < pathlist.Length; i++)
                {
                    string branddayspath = pathlist[i];  //一个品牌多天数据信息
                    string[] branddayspathList = branddayspath.Split(';');
                    if (branddayspathList.Length > 0)
                    {
                        for (int j = 0; j < branddayspathList.Length - 1; j++)
                        {
                            document.Add(new Paragraph("品牌： " + branddayspathList[branddayspathList.Length - 1].Substring(1), fontChinese));
                            bool flag = false;
                            string[] brandadaypathList = branddayspathList[j].Split('|'); //一个品牌一天的数据
                            if (brandadaypathList.Length > 0)
                            {
                                flag = true;
                                document.Add(new Paragraph("时间： " + brandadaypathList[brandadaypathList.Length - 1], fontChinese));
                                int len = brandadaypathList.Length - 1;
                                if (len > 4)
                                    len = 4;
                                for (int m = 0; m < len; m++)
                                {
                                    string photoPath = brandadaypathList[m];
                                    if (File.Exists(photoPath))
                                    {
                                        FileStream fs = new FileStream(photoPath, FileMode.Open, FileAccess.Read);
                                        if (fs.Length != 0)
                                        {
                                            iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(photoPath);
                                            if (jpeg01 == null)
                                            {
                                                continue;
                                            }
                                            if (jpeg01.Height > jpeg01.Width)
                                                jpeg01.SetAbsolutePosition(45f + (m % 2) * (5f + w), document.PageSize.Height - (h + 95f) * (1 + m / 2));
                                            else
                                                jpeg01.SetAbsolutePosition(45f + (m % 2) * (5f + w), document.PageSize.Height - (h + 20f) * (1 + m / 2));
                                            jpeg01.ScaleToFit(w, h);
                                            document.Add(jpeg01);
                                        }
                                    }
                                }
                            }

                            PdfContentByte cb = writer.DirectContent;
                            if (note.Length > 28)
                            {
                                Phrase footer1 = new Phrase("备注: " + note.Substring(0, 28), fontChinese);
                                ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer1,
                                250, document.Bottom + 26f, 0);
                                Phrase footer2 = new Phrase(note.Substring(28), fontChinese);
                                ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer2,
                                250, document.Bottom + 14f, 0);
                            }
                            else
                            {
                                Phrase footer = new Phrase("备注: " + note, fontChinese);
                                ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer,
                                150, document.Bottom + 20, 0);
                            }
                            if (flag)
                                document.NewPage();//每个品牌每天的信息占一页
                        }
                    }
                }
            }
            document.Close();
            byte[] pdfmslist = pdfms.ToArray();
            dicMStream.Add(customerName + ".pdf", pdfmslist);
        }
        catch (DocumentException de)
        {
            Response.Write(de.ToString());
        }

        return pdfms;
    }

    MemoryStream WriteToStream(string filename)
    {
        Document document = new Document();
        MemoryStream file = new MemoryStream();
        //hssfworkbook.Write(file);
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
        return file;
    }

    /// <summary>
    /// 根据时间创建文件
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <returns></returns>
    private string createFile(string path)
    {
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        return path;
    }



    /// <summary>
    /// 删除过期文件
    /// </summary>
    /// <param name="dir">目录路径</param>
    /// <param name="delname">待删除文件或文件夹名称</param>
    public void DeleteFolder(string dir)
    {
        try
        {
            string[] Directorys = Directory.GetDirectories(dir);  //获得子目录
            if (Directorys == null)
                return;
            foreach (string file in Directorys)
            {
                FileInfo fileinfo = new FileInfo(file);
                if (Directory.Exists(file) && fileinfo.CreationTime.AddDays(-7) < DateTime.Now)
                {
                    Directory.Delete(file, true);
                }
            }
        }
        catch (Exception )
        { }
    }

    #region 下载并压缩
    ZipOutputStream zos = null;
    String strBaseDir = "";
    Dictionary<string, byte[]> dicMStream;
    /// <summary>
    /// 下载并压缩
    /// </summary>
    /// <param name="strPath">文件路径</param>
    /// <param name="strFileName">压缩包名称</param>
    //void dlZipDir(string strPath, string strFileName)
    //{
    //    MemoryStream ms = null;
    //    Response.ContentType = "application/octet-stream";
    //    strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
    //    Response.AddHeader("Content-Disposition", "attachment;   filename=" + strFileName + ".zip");
    //    ms = new MemoryStream();
    //    zos = new ZipOutputStream(ms);
    //    strBaseDir = strPath + "";
    //    addZipEntry(strBaseDir);
    //    zos.Finish();
    //    zos.Close();
    //    Response.Clear();
    //    Response.BinaryWrite(ms.ToArray());
    //    Response.End();
    //}



    void dlZipDir( string strFileName)
    {
        MemoryStream ms = null;
        Response.ContentType = "application/octet-stream";
        strFileName = HttpUtility.UrlEncode(strFileName).Replace('+', ' ');
        Response.AddHeader("Content-Disposition", "attachment;   filename=" + strFileName + ".zip");
        ms = new MemoryStream();
        zos = new ZipOutputStream(ms);
        addZipEntry(dicMStream);
        zos.Finish();
        zos.Close();
        Response.Clear();
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }

    //void addZipEntry(string PathStr)
    //{
    //    DirectoryInfo di = new DirectoryInfo(PathStr);
    //    foreach (DirectoryInfo item in di.GetDirectories())
    //    {
    //        addZipEntry(item.FullName);
    //    }
    //    foreach (FileInfo item in di.GetFiles())
    //    {
    //        FileStream fs = File.OpenRead(item.FullName);
    //        byte[] buffer = new byte[fs.Length];
    //        fs.Read(buffer, 0, buffer.Length);
    //        string strEntryName = item.FullName.Replace(strBaseDir, "");
    //        ZipEntry entry = new ZipEntry(strEntryName);
    //        zos.PutNextEntry(entry);
    //        zos.Write(buffer, 0, buffer.Length);
    //        fs.Close();
    //    }
    //}


    /// <summary>
    /// 内存中读取PDF流
    /// </summary>
    /// <param name="dic">PDF文件名和Memorystream的内容</param>
    void addZipEntry(Dictionary<string, byte[]> dic)
    {
        foreach (string strEntryName in dic.Keys)
        {
            ZipEntry entry = new ZipEntry(strEntryName);
            zos.PutNextEntry(entry);
            zos.Write(dic[strEntryName], 0, dic[strEntryName].Length);
        }
    }
    #endregion

}